package com.simple.config;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：foursmile-local-etl
 * 类名称：SyncConfigFactory
 * 类描述：SyncConfigFactory
 * 创建时间：2022/10/18
 *
 * @author jiangjunjie   (E-mail:jiangjunjie@foursmile001.com)
 * @version v1.0
 */
public class SyncConfigFactory {

    private Integer index = 1;

    public SyncConfig createByResource(String path) {
        String syncJson = ResourceUtil.readUtf8Str(path);
        return this.createByJson(syncJson);
    }

    public SyncConfig createByJson(String syncJson) {
        SyncConfig syncConfig = JSONUtil.toBean(syncJson, SyncConfig.class);

        String sourceEnv = syncConfig.getSourceEnv();
        String targetEnv = syncConfig.getTargetEnv();
        if (sourceEnv == null) {
            throw new RuntimeException("sourceEnv不能为空");
        }
        if (targetEnv == null) {
            throw new RuntimeException("targetEnv不能为空");
        }

        for (SyncTaskConfig syncTaskConfig : syncConfig.getTasks()) {
            init(syncTaskConfig);
        }
        return syncConfig;
    }

    private void init(SyncTaskConfig syncTaskConfig) {
        String taskName = syncTaskConfig.getTaskName();
        if (taskName == null) {
            syncTaskConfig.setTaskName("taskName" + index++);
        }


        String selectSql = syncTaskConfig.getSelectSql();

        if (selectSql == null) {
            throw new RuntimeException("selectSql不能为空");
        }
        if (syncTaskConfig.getTargetSelectSql() == null) {
            syncTaskConfig.setTargetSelectSql(selectSql);
        }

        if (syncTaskConfig.getTableName() == null || syncTaskConfig.getTableName().isEmpty()) {
            try {
                Select targetSelectSql = (Select) CCJSqlParserUtil.parse(syncTaskConfig.getTargetSelectSql());
                PlainSelect selectBody = targetSelectSql.getSelectBody(PlainSelect.class);
                Table table = (Table) selectBody.getFromItem();
                String tableName = table.getName();
                syncTaskConfig.setTableName(tableName);
            } catch (JSQLParserException e) {
                throw new RuntimeException(e);
            }
        }

        {//校验初始化比对策略
            SyncTaskConfig.ContrastStrategy contrastStrategy = syncTaskConfig.getContrastStrategy();
            List<String> primaryKey = contrastStrategy.getPrimaryKey();
            if (primaryKey == null || primaryKey.isEmpty()) {
                throw new RuntimeException("比对策略:指定主键不可为空");
            }
            List<String> insertIgnore = contrastStrategy.getInsertIgnore();
            if (insertIgnore == null) {
                contrastStrategy.setInsertIgnore(new ArrayList<>());
            }
            List<String> updateColumns = contrastStrategy.getUpdateColumns();
            if (updateColumns == null) {
                contrastStrategy.setUpdateColumns(new ArrayList<>());
            }
        }


    }


}
